import 'package:flutter/material.dart';
import 'package:flutter_youren/AppConstant.dart';
import 'package:flutter_youren/main.dart';
import 'package:flutter_youren/pages/AccounDetailPage.dart';
import 'package:flutter_youren/pages/AppContants.dart';
import 'package:flutter_youren/pages/LoginPage.dart';
import 'package:flutter_youren/pages/SettingPage.dart';
import 'package:flutter_youren/pages/SharedPage.dart';
import 'package:flutter_youren/pages/UnknownRoute.dart';
import 'package:flutter_youren/pages/VideoPage.dart';
import 'package:flutter_youren/pages/main/HomeWidget.dart';
import 'package:flutter_youren/util/LogUtils.dart';
import 'package:flutter_youren/util/MMKVUtil.dart';
import 'package:mmkv/mmkv.dart';

import 'pages/ImagePage.dart';
import 'pages/MusicPage.dart';

/// 路由的动画效果1：http://t.zoukankan.com/joe235-p-11230780.html
/// 路由的动画效果2： https://www.freesion.com/article/87611133468/
class NoAnimationMaterialPageRoute<T> extends MaterialPageRoute<T> {
  NoAnimationMaterialPageRoute({
    required super.builder,
    super.settings,
  });

  @override
  Widget buildTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return child;
  }
}

class RouteConfiguration {
  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    LogUtils.d(
        "RouteConfiguration", "onGenerateRoute settings.name ${settings.name}");
    return MaterialPageRoute(builder: (context) {
      var routeName = settings.name;
      StatelessWidget rout = UnknownRoute(routeName);
      switch (routeName) {
        case ACCOUNT_DETAIL_PAGE:
          var loginState = MMKVUtil.instance.getString(LOGIN_STATE);
          if (loginState == "1") {
            rout = const AccountDetailPage();
          } else {
            rout = const LoginPage();
          }
          break;
        default:
      }
      return rout;
      // 如果访问的路由页需要登录，但当前未登录，则直接返回登录页路由，
      // 引导用户登录；其它情况则正常打开路由。
    });
  }

  static Route<dynamic>? onUnknownRoute(RouteSettings settings) {
    LogUtils.d("RouteConfiguration",
        "onUnknownRoute  settings.name = ${settings.name}");
    return MaterialPageRoute(builder: (context) {
      var routeName = settings.name;
      StatelessWidget route = UnknownRoute(routeName);
      return route;
    });
  }

  static Map<String, WidgetBuilder> routes() {
    LogUtils.d("RouteConfiguration", "routes");
    return {
      VIDEO_PAGE: (context) => const VideoPage(),
      IMAGE_PAGE: (context) => const ImagePage(),
      MUSIC_PAGE: (context) => const MusicPage(),
      SHARED_PAGE: (context) => const SharedPage(),
      SETTING_PAGE: (context) => const SettingPage(),
      MYHOME_PAGE: (context) => const MyHomePage(title: "home"),
      A_PAGE: (context) => const A(),
    };
  }
}

class CustomRoute extends PageRouteBuilder {
  final Widget widget;

  //构造方法
  CustomRoute(this.widget)
      : super(
            transitionDuration: const Duration(seconds: 2), //过渡时间
            pageBuilder: (
              //构造器
              BuildContext context,
              Animation<double> animation1,
              Animation<double> animation2,
            ) {
              return widget;
            },
            transitionsBuilder: (BuildContext context,
                Animation<double> animation1,
                Animation<double> animation2,
                Widget child) {
              return ScaleTransition(
                scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                  parent: animation1,
                  curve: Curves.fastOutSlowIn,
                )),
                child: child,
              );
            });
}
